/**
 * 
 */
package com.gopay.webtest.util;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

/**
 * @RequestUtil.java
 * @author Ryan
 * @2013-9-27 上午8:59:55 www.gopay.com.cn Inc.All rights reserved.
 * @param <T>
 */
public class RequestUtil<T> {

    /** UTF-8编码 */
    public static final String ENCODE_UTF8 = "UTF-8";
    /** ISO8859-1编码 */
    public static final String ENCODE_ISO = "ISO8859-1";
    /** GBK编码 */
    public static final String ENCODE_GBK = "GBK";
    /** 编码类型：1 GBK */
    public static final String ENCODE_TYPE_1 = "1";
    /** 编码类型：2 UTF8 */
    public static final String ENCODE_TYPE_2 = "2";

    private final static Logger logger = Logger.getLogger(RequestUtil.class);

    public static Map<String, String> getParamsFromRequest(HttpServletRequest request) {
        Map<String, String> params = new HashMap<String, String>();
        for (Enumeration enumeration = request.getParameterNames(); enumeration.hasMoreElements();) {
            String eleName = (String) enumeration.nextElement();
            String eleValue = request.getParameter(eleName);
            params.put(eleName, eleValue);
            logger.error("WEBTEST收到的请求参数键 : [" + eleName + "]，值: [" + eleValue + "]");
        }
        return params;
    }

    public static Map<String, String> setCharset(Map<String, String> params) throws UnsupportedEncodingException {
        String charSet = params.get("charset")==null ? ENCODE_GBK :params.get("charset");
        String newChar =  "1".equals(charSet) ? ENCODE_GBK : ENCODE_UTF8;
        String goodsDetail = params.get("goodsDetail");
        Map<String, String> resultMap = new HashMap<String, String>();
        if (null != goodsDetail && StringUtils.isNotBlank(goodsDetail) && StringUtils.isNotEmpty(goodsDetail)) {
            if(ENCODE_UTF8.equals(charSet))
                resultMap.put("goodsDetail", new String(goodsDetail.getBytes(), newChar));
            else
                resultMap.put("goodsDetail", new String(goodsDetail.getBytes(), newChar));
                
        }

        String msgExt = params.get("msgExt");
        if (null != msgExt && StringUtils.isNotBlank(msgExt) && StringUtils.isNotEmpty(msgExt)) {
            if (ENCODE_UTF8.equals(charSet))
            resultMap.put("msgExt", new String(msgExt.getBytes(ENCODE_UTF8), newChar));
            else
                resultMap.put("msgExt", new String(msgExt.getBytes(ENCODE_GBK), newChar));
        }

        String buyerName = params.get("buyerName");
        if (null != buyerName && StringUtils.isNotBlank(buyerName) && StringUtils.isNotEmpty(buyerName)) {
            if(ENCODE_UTF8.equals(charSet))
                resultMap.put("buyerName", new String(buyerName.getBytes(ENCODE_UTF8), newChar));
            else
                resultMap.put("buyerName", new String(buyerName.getBytes(ENCODE_GBK), newChar));
                
        }

        String merRemark2 = params.get("merRemark2");
        if (null != merRemark2 && StringUtils.isNotBlank(merRemark2) && StringUtils.isNotEmpty(merRemark2)) {
            if(ENCODE_UTF8.equals(charSet))
                resultMap.put("merRemark2", new String(merRemark2.getBytes(ENCODE_UTF8), newChar));
            else
                resultMap.put("merRemark2", new String(merRemark2.getBytes(ENCODE_GBK), newChar));
                
        }

        String merRemark1 = params.get("merRemark1");
        if (null != merRemark1 && StringUtils.isNotBlank(merRemark1) && StringUtils.isNotEmpty(merRemark1)) {
            if(ENCODE_UTF8.equals(charSet))
            resultMap.put("merRemark1", new String(merRemark1.getBytes(ENCODE_UTF8), newChar));
            else
                resultMap.put("merRemark1", new String(merRemark1.getBytes(ENCODE_GBK), newChar));
                
        }

        String goodsName = params.get("goodsName");
        if (null != goodsName && StringUtils.isNotBlank(goodsName) && StringUtils.isNotEmpty(goodsName)) {
            if(ENCODE_UTF8.equals(charSet))
            resultMap.put("goodsName", new String(goodsName.getBytes(ENCODE_UTF8), newChar));
            else
                resultMap.put("goodsName", new String(goodsName.getBytes(ENCODE_GBK), newChar));
                
        }

        String buyerContact = params.get("buyerContact");
        if (null != buyerContact && StringUtils.isNotBlank(buyerContact) && StringUtils.isNotEmpty(buyerContact)) {
            if(ENCODE_UTF8.equals(charSet))
                resultMap.put("buyerContact", new String(buyerContact.getBytes(ENCODE_UTF8), newChar));
            else
                resultMap.put("buyerContact", new String(buyerContact.getBytes(ENCODE_GBK), newChar));
                
        }

        return resultMap;
    }

    public   T  convertCharsetForObjectField(T t, HttpServletRequest request) throws IllegalArgumentException, IllegalAccessException {

        Map<String, String> param;
        try {
            param = setCharset(getParamsFromRequest(request));
        } catch (UnsupportedEncodingException e) {
            return null;
        }
        Field[] fields = t.getClass().getDeclaredFields();

        for (Entry<String, String> entry : param.entrySet()) {

            for (Field field : fields) {
                if (entry.getKey().equals(field.getName())) {
                    field.setAccessible(true);
                    field.set(t, entry.getValue());
                }
            }
        }
        return t;
    }

}
